package com.shuyuntu.proposal.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shuyuntu.proposal.enums.EProposalState;
import com.shuyuntu.proposal.enums.EProposalType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 代表了提案实体的类
 * Created by limeng on 16/4/15.
 */
@Entity
@Table(name = "proposal")
public class Proposal {
    public Proposal() {
    }

    /**
     * 提案的主键
     */
    private Integer id;

    /**
     * 提案的标题
     */
    private String title;

    /**
     * 提案的内容
     */
    private String content;

    /**
     * 提案的作者
     */
    private Delegate author;

    /**
     * 提案创建的时间
     */
    private Date createTime;

    /**
     * 提案修改的时间
     */
    private Date updateTime;

    /**
     * 提案发布的时间
     */
    private Date publishTime;

    /**
     * 提案的状态
     * (草稿/待审核等)
     */
    private EProposalState proposalState;

    /**
     * 提案的类型
     * (财务/教务/管理等)
     */
    private EProposalType proposalType;

    /**
     * 拒绝通过初审的理由
     */
    private String refuseResult;

    /**
     * 教师代表反馈信息
     */
    private String feedbackInfo;

    /**
     * 附议信息列表
     */
    @JsonIgnore
    private Set<SecondMotion> secondMotions = new HashSet<>();

    /**
     * 附件信息列表
     */
    private Set<Attachment> attachments = new HashSet<>();

    /**
     * 主办部门
     */
    private Department hostDepartment;

    /**
     * 协办部门
     */
    private Set<Department> assistDepartments = new HashSet<>();

    /**
     * 点击次数
     */
    private int clickCount;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(length = 30, nullable = false)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Basic(fetch = FetchType.LAZY)
    @Column(length = 10000, nullable = false)
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @ManyToOne(targetEntity = Delegate.class)
    @JoinColumn(name = "author_id", nullable = false)
    public Delegate getAuthor() {
        return author;
    }

    public void setAuthor(Delegate author) {
        this.author = author;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_time", nullable = false)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "update_time", nullable = false)
    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "publish_time")
    public Date getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(Date publishTime) {
        this.publishTime = publishTime;
    }

    @Enumerated(EnumType.ORDINAL)
    @Column(name = "proposal_state", nullable = false, columnDefinition = "int default 0")
    public EProposalState getProposalState() {
        return proposalState;
    }

    public void setProposalState(EProposalState proposalState) {
        this.proposalState = proposalState;
    }

    @Enumerated(EnumType.ORDINAL)
    @Column(name = "proposal_type")
    public EProposalType getProposalType() {
        return proposalType;
    }

    public void setProposalType(EProposalType proposalType) {
        this.proposalType = proposalType;
    }

    @ManyToOne(targetEntity = Department.class)
    @JoinColumn(name = "host_department_id")
    @NotFound(action = NotFoundAction.IGNORE)
    public Department getHostDepartment() {
        return hostDepartment;
    }

    public void setHostDepartment(Department hostDepartment) {
        this.hostDepartment = hostDepartment;
    }

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "proposal__assist_department",
            joinColumns = {@JoinColumn(name = "proposal_id")},
            inverseJoinColumns = {@JoinColumn(name = "assist_id")})
    public Set<Department> getAssistDepartments() {
        return assistDepartments;
    }

    public void setAssistDepartments(Set<Department> assistDepartments) {
        this.assistDepartments = assistDepartments;
    }

    @Column(name = "click_count", columnDefinition = "int default 0", nullable = false)
    public int getClickCount() {
        return clickCount;
    }

    public void setClickCount(int clickCount) {
        this.clickCount = clickCount;
    }

    @OneToMany(mappedBy = "proposal")
    public Set<Attachment> getAttachments() {
        return attachments;
    }

    public void setAttachments(Set<Attachment> attachments) {
        this.attachments = attachments;
    }

    @Column(name = "refuse_result", length = 255)
    public String getRefuseResult() {
        return refuseResult;
    }

    public void setRefuseResult(String refuseResult) {
        this.refuseResult = refuseResult;
    }

    @OneToMany(mappedBy = "targetProposal")
    public Set<SecondMotion> getSecondMotions() {
        return secondMotions;
    }

    public void setSecondMotions(Set<SecondMotion> secondMotions) {
        this.secondMotions = secondMotions;
    }

    @Column(name = "feedback_info")
    public String getFeedbackInfo() {
        return feedbackInfo;
    }

    public void setFeedbackInfo(String feedbackInfo) {
        this.feedbackInfo = feedbackInfo;
    }
}
